package com.atguigu.flink.watermark;

import com.atguigu.flink.function.WaterSensorMapFunction;
import com.atguigu.flink.pojo.WaterSensor;
import com.atguigu.flink.utils.MyUtil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessAllWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.time.Duration;

/**
 * Created by Smexy on 2023/4/8
 *
 *  下游的watermark会由于上游的某个Task的watermark不推进，而导致停滞。
 *
 *          如何处理？
 *              可以设置 WatermarkStrategy.withIdleness(空闲时间 t)
 *
 *              某个Task，超过了t时间，都没有收到数据，此时这个Task就退出水印的发送。
 *              后续，只要这个Task又收到了数据，就会恢复水印的发送。
 */
public class Demo6_WaterMarkIdle
{
    public static void main(String[] args) throws Exception {

        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 3333);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);

        env.getConfig().setAutoWatermarkInterval(2000);
        env.disableOperatorChaining();
        env.setParallelism(6);

        WatermarkStrategy<WaterSensor> watermarkStrategy = WatermarkStrategy
            .<WaterSensor>forMonotonousTimestamps()
            .withTimestampAssigner( (e, ts) -> e.getTs())
            .withIdleness(Duration.ofSeconds(10));


        env
            .socketTextStream("hadoop102", 8888)
            .global()
            .map(new WaterSensorMapFunction()).name("myMap")
            .assignTimestampsAndWatermarks(watermarkStrategy)
            .windowAll(TumblingEventTimeWindows.of(Time.seconds(5)))
            .process(new ProcessAllWindowFunction<WaterSensor, String, TimeWindow>()
            {
                @Override
                public void process(Context context, Iterable<WaterSensor> elements, Collector<String> out) throws Exception {

                    out.collect(context.window() + ":" + MyUtil.parseToList(elements));

                }
            })
            .print();


            env.execute();

    }
}
